package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.SetNameDataBean;
import com.fancy.learncenter.bean.UpHeaderBean;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class SettingNameActivity extends BaseActivity {

    @Bind(R.id.name)
    EditText name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_name, Status.STATUS, "修改昵称");
        ButterKnife.bind(this);
        name.setText(LoginUserSharePrefernce.getName());
        name.setSelection(LoginUserSharePrefernce.getName().length());
        setRight("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.show("保存成功");

                if (!TextUtils.isEmpty(name.getText())) {
                    setName(name.getText().toString());
                } else {
                    ToastUtil.show("昵称不能为空");
                }
            }
        });
    }


    private void setName(final String name) {
        HttpMehtod.getInstance().setName(name, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<SetNameDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), SetNameDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<SetNameDataBean>() {
                    @Override
                    public void success(SetNameDataBean dataBean) {
                        if (dataBean.getResult() == 1) {
                            ToastUtil.show("昵称修改成功");
                            LoginUserSharePrefernce.putName(name);
                            LoginUserSharePrefernce.isUpdate();
                            finish();
                        } else {
                            ToastUtil.show("昵称修改失败");
                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }

                }).dealData();
            }
        });
    }
}
